/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
*/
package net.redxyz.repost.manage.action.app;

import net.redxyz.top.dbo.sys.SysPermission;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 应用信息管理
 */
public class PermissionAction extends StdSwitchAction
{

    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("permissionId", "请选择一个选项"));
    }
    
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("permissionId", "请选择应用编码"));        
        request.addValidate(new IsLen("permissionName", "应用名称不能为空且不能超过32个汉字", 1, 32));
        request.addValidate(new IsIntegerValue("permissionSort", "应用排序范围1-9999", 1, 9999));
        request.addValidate(new IsLen("permissionUrl", "应用URL不能为空且不能超过512个汉字", 1, 512));
        request.addValidate(new IsLen("permissionDesc", "应用描述不能超过127个汉字", 0, 127)); 
    }

    protected void list(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        
        int page = request.getParameterInt("page", 1);
        int pageSize = 20;

        String permissionId = request.getParameter("permissionId");
        String permissionName = request.getParameter("permissionName");
       
        Selector sel = new Selector();
        sel.addMaybeLike("permissionId", permissionId);
        sel.addMaybeLike("permissionName", permissionName);
        sel.addOrderbyAsc("permissionId");
        PageResult<SysPermission> pageResult = ORM.table().page(SysPermission.class, page, pageSize, sel);                
        pageResult.addConditionMap(request.getParameterMap());
        
        request.setAttribute("pageResult", pageResult);
    }

    protected void add(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        
      
    }

    protected void modify(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        
        String permissionId = request.getParameter("permissionId");
        SysPermission item = ORM.table().item(SysPermission.class, permissionId);
        if (item == null)
        {
            request.returnHistory("您选择的应用编码不存在");            
            return;
        }
        
        request.setAttribute("item", item);
    }

    protected void insert(HttpRequest request) throws Exception
    {      
        String permissionId = request.getParameter("permissionId");
        String permissionName = request.getParameter("permissionName");
        int permissionStatus = request.getParameterInt("permissionStatus");
        int permissionRule = request.getParameterInt("permissionRule");
        int permissionSort = request.getParameterInt("permissionSort");
        String permissionUrl = request.getParameter("permissionUrl");
        int permissionRule2 = request.getParameterInt("permissionRule2");
        String permissionRule2Date = request.getParameter("permissionRule2Date");
        String permissionDesc = request.getParameter("permissionDesc");
        
        SysPermission item = ORM.table().item(SysPermission.class, permissionId);
        if (item != null)
        {
            request.returnHistory("该应用编码已经存在，请重新输入");            
            return;
        }
        
        SysPermission permission = new SysPermission();
      
        permission.setPermissionId(permissionId);
        permission.setPermissionName(permissionName);
        permission.setPermissionStatus(permissionStatus);
        permission.setPermissionRule(permissionRule);
        permission.setPermissionSort(permissionSort);
        permission.setPermissionUrl(permissionUrl);
        permission.setPermissionOperateTime(DateTimes.getDateTimeString());
        permission.setPermissionRule2(permissionRule2);
        permission.setPermissionRule2Date(permissionRule2Date);
        permission.setPermissionDesc(permissionDesc);
        
        ORM.table().insert(permission);
    }

    protected void update(HttpRequest request) throws Exception
    {
        String permissionId = request.getParameter("permissionId");
        String permissionName = request.getParameter("permissionName");
        int permissionStatus = request.getParameterInt("permissionStatus");
        int permissionRule = request.getParameterInt("permissionRule");
        int permissionSort = request.getParameterInt("permissionSort");
        String permissionUrl = request.getParameter("permissionUrl");
        int permissionRule2 = request.getParameterInt("permissionRule2");
        String permissionRule2Date = request.getParameter("permissionRule2Date");
        String permissionDesc = request.getParameter("permissionDesc");
        
        SysPermission item = ORM.table().item(SysPermission.class, permissionId);
        if (item != null)
        {
            Updater updater = new Updater();
            updater.addMust("permissionId", permissionId);
            updater.addField("permissionName", permissionName);
            updater.addField("permissionStatus", permissionStatus);
            updater.addField("permissionRule", permissionRule);
            updater.addField("permissionSort", permissionSort);
            updater.addField("permissionUrl", permissionUrl);
            updater.addField("permissionRule2", permissionRule2);
            updater.addField("permissionRule2Date", permissionRule2Date);
            updater.addField("permissionOperateTime", DateTimes.getDateTimeString());
            updater.addField("permissionDesc", permissionDesc);
            
            ORM.table().update(SysPermission.class, updater);
        }
    }

    protected void delete(HttpRequest request) throws Exception
    {
        String permissionId = request.getParameter("permissionId");
        
        ORM.table().delete(SysPermission.class, permissionId);
    }

}
